package com.wjy.icu.task;

import com.wjy.icu.domain.*;
import com.wjy.icu.domain.vo.BluetoothVo;
import com.wjy.icu.emqx.utils.MqttUtils;
import com.wjy.icu.mapper.EquipmentMapper;
import com.wjy.icu.mapper.PresetModelsConditionMapper;
import com.wjy.icu.mapper.WdtMonitoredMapper;
import com.wjy.icu.service.IBluetoochService;
import com.wjy.icu.service.IMqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: wangjunyong
 * @date: 2021/11/1 12:07
 */
@Component
@EnableScheduling
public class MonitoredCheck0FTask {

    @Autowired
    IMqttService mqttService;
    @Autowired
    IBluetoochService bluetoochService;
    @Autowired
    WdtMonitoredMapper wdtMonitoredMapper;
    @Autowired
    PresetModelsConditionMapper presetModelsConditionMapper;
    @Autowired
    EquipmentMapper equipmentMapper;

    @Scheduled(cron = "0/5 * * * * ?") //每3秒执行一次
    @Transactional
    public void configureTasks() throws InterruptedException {
        EquipmentExample example = new EquipmentExample();
        example.createCriteria().andStatusEqualTo(1);
        List<Equipment> equipmentList = equipmentMapper.selectByExample(example);
        equipmentList.stream().forEach(equipment -> {
            PresetModelsConditionExample aa = new PresetModelsConditionExample();
            aa.createCriteria().andEquitmentIdEqualTo(equipment.getId()).andModelEqualTo(1).andBiggerGreaterThanOrEqualTo(new BigDecimal(38));
            aa.orderBy("bigger");
            List<PresetModelsCondition> presetModelsConditions = presetModelsConditionMapper.selectByExample(aa);
            if (presetModelsConditions.size()>0){
                PresetModelsCondition presetModelsCondition = presetModelsConditions.get(0);
                String setting = presetModelsCondition.getThermostatTreatmentTemperatureSetting();
                String seting  ="";
                if (presetModelsCondition.getBigger()!=null){
                    seting = presetModelsCondition.getBigger().toString();
                }else {
                    seting = presetModelsCondition.getLower().toString();
                }
                //发送宠物设置温度
                String s7 = MqttUtils.lowHigh(setting);
                String s = MqttUtils.lowHigh(seting);
                mqttService.sendEquitment(equipment.getMac(), "0F", s+s7);
            }else {
                PresetModelsConditionExample bb = new PresetModelsConditionExample();
                bb.createCriteria().andEquitmentIdEqualTo(equipment.getId()).andModelEqualTo(2).andLowerLessThanOrEqualTo(new BigDecimal(35));
                bb.orderBy("lower desc");
                List<PresetModelsCondition> presetModelsConditionList = presetModelsConditionMapper.selectByExample(bb);
                if (presetModelsConditionList.size()>0){
                    PresetModelsCondition presetModelsCondition = presetModelsConditionList.get(0);
                    String setting = presetModelsCondition.getThermostatTreatmentTemperatureSetting();
                    String seting  ="";
                    if (presetModelsCondition.getBigger()!=null){
                        seting = presetModelsCondition.getBigger().toString();
                    }else {
                        seting = presetModelsCondition.getLower().toString();
                    }
                    //发送宠物设置温度
                    String s7 = MqttUtils.lowHigh(setting);
                    String s = MqttUtils.lowHigh(seting);
                    mqttService.sendEquitment(equipment.getMac(), "0F", s+s7);
                }
            }
        });
    }


    private  void sendTemp(String mac,String temp,Integer equipmentId){
        BigDecimal decimal = new BigDecimal(temp);
        PresetModelsConditionExample example = new PresetModelsConditionExample();
        example.createCriteria().andEquitmentIdEqualTo(equipmentId).andModelEqualTo(1);
        example.orderBy("bigger desc");
        List<PresetModelsCondition> presetModelsConditions = presetModelsConditionMapper.selectByExample(example);
        int aa=-1;
        if (presetModelsConditions.size()>0){
            for (int i = 0; i < presetModelsConditions.size(); i++) {
                if (i<presetModelsConditions.size()-1) {
                    PresetModelsCondition p1 = presetModelsConditions.get(i);
                    PresetModelsCondition p2 = presetModelsConditions.get(i + 1);
                    if (p1.getBigger() != null && p2.getBigger() != null && p1.getBigger().compareTo(decimal) > 0 && p2.getBigger().compareTo(decimal) <= 0) {
                        aa = i;
                    }
                }
            }
            if (aa!=-1) {
                PresetModelsCondition presetModelsCondition = presetModelsConditions.get(aa);
                String setting = presetModelsCondition.getThermostatTreatmentTemperatureSetting();
                //发送宠物设置温度
                String s7 = MqttUtils.lowHigh(setting);
                mqttService.sendEquitment(mac, "0F", s7);
            }
        }
        PresetModelsConditionExample example1 = new PresetModelsConditionExample();
        example1.createCriteria().andEquitmentIdEqualTo(equipmentId).andModelEqualTo(2);
        example1.orderBy("lower desc");
        List<PresetModelsCondition> presetModelsCondition1s = presetModelsConditionMapper.selectByExample(example1);
        int aa1=-1;
        if (presetModelsCondition1s.size()>0){
            for (int i = 0; i < presetModelsCondition1s.size(); i++) {
                if (i<presetModelsCondition1s.size()-1) {
                    PresetModelsCondition p1 = presetModelsCondition1s.get(i);
                    PresetModelsCondition p2 = presetModelsCondition1s.get(i + 1);
                    if (p1.getLower() != null && p2.getLower() != null && p1.getLower().compareTo(decimal) > 0 && p2.getLower().compareTo(decimal) <= 0) {
                        aa1 = i;
                    }
                }
            }
            if (aa1!=-1) {
                PresetModelsCondition presetModelsCondition = presetModelsCondition1s.get(aa1);
                String setting = presetModelsCondition.getThermostatTreatmentTemperatureSetting();
                String seting  ="";
                if (presetModelsCondition.getBigger()!=null){
                    seting = presetModelsCondition.getBigger().toString();
                }else {
                    seting = presetModelsCondition.getLower().toString();
                }
                //发送宠物设置温度
                String s7 = MqttUtils.lowHigh(setting);
                String s = MqttUtils.lowHigh(seting);
                mqttService.sendEquitment(mac, "0F", s7+s);
            }
        }

    }
    private  void sendTemp1(String mac,String temp,Integer equipmentId){
        BigDecimal decimal = new BigDecimal(temp);
        PresetModelsConditionExample example = new PresetModelsConditionExample();
        example.createCriteria().andEquitmentIdEqualTo(equipmentId);
        example.orderBy("bigger,lower");
        List<PresetModelsCondition> presetModelsConditions = presetModelsConditionMapper.selectByExample(example);
        PresetModelsCondition presetModelsCondition = presetModelsConditions.get(0);
        if (decimal.compareTo(new BigDecimal(38))>0){
            //高温预警
            List<PresetModelsCondition> collect = presetModelsConditions.stream().filter(x -> x.getModel() == 1).collect(Collectors.toList());
            int aa = 0;
            for (int i = 0; i < collect.size(); i++) {
                if (i<collect.size()-1) {
                    PresetModelsCondition p1 = presetModelsConditions.get(i);
                    PresetModelsCondition p2 = presetModelsConditions.get(i + 1);
                    if (p1.getBigger() != null && p2.getBigger() != null && p1.getBigger().compareTo(decimal)< 0 && p2.getBigger().compareTo(decimal) >=0) {
                        aa = i;
                    }
                }
            }
            if (aa!=-1) {
                PresetModelsCondition p1 = collect.get(aa);
                String setting = p1.getThermostatTreatmentTemperatureSetting();
                String seting  ="";
                if (p1.getBigger()!=null){
                    seting = p1.getBigger().toString();
                }
                //发送宠物设置温度
                String s7 = MqttUtils.lowHigh(setting);
                String s = MqttUtils.lowHigh(seting);
                mqttService.sendEquitment(mac, "0F", s+s7);
            }
        }else {
            //低温预警
            List<PresetModelsCondition> collect = presetModelsConditions.stream().filter(x -> x.getModel() == 2).collect(Collectors.toList());
            int aa = 0;
            for (int i = 0; i < collect.size(); i++) {
                if (i<collect.size()-1) {
                    PresetModelsCondition p1 = presetModelsConditions.get(i);
                    PresetModelsCondition p2 = presetModelsConditions.get(i + 1);
                    if (p1.getLower() != null && p2.getLower() != null && p1.getLower().compareTo(decimal) < 0 && p2.getLower().compareTo(decimal) >= 0) {
                        aa = i;
                    }
                }
            }
            if (aa!=-1) {
                PresetModelsCondition p1 = collect.get(aa);
                String setting = p1.getThermostatTreatmentTemperatureSetting();
                String seting  ="";
                if (p1.getLower()!=null){
                    seting = p1.getLower().toString();
                }
                //发送宠物设置温度
                String s7 = MqttUtils.lowHigh(setting);
                String s = MqttUtils.lowHigh(seting);
                mqttService.sendEquitment(mac, "0F", s+s7);
            }
        }
    }

}
